<%layout("/common/_layout.html"){ %>
<%#ajax userTable:{%>
<div class="am-cf am-padding">
    <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">业务配置</strong> /
        <small>业务大类管理</small>
    </div>
</div>
<div class="am-g">
    <div class="am-u-sm-12">
        <form class="am-form" id="list-form" action="${base}/tariffClass/list" method="post">
            <input type="hidden" id="keywords" name="keywords" value='${keywords!""}'/>
            <input type="hidden" id="isExport" name="isExport" value='${isExport!"0"}'/>
            <table class="am-table am-table-bordered am-table-radius table-main" style="padding:0;">
                <tbody>
                <tr>
                    <th class="am-text-middle">所属活动</th>
                    <td>
                        <div class="am-u-sm-12">
                            <select select data-am-selected="{btnWidth: '100%',maxHeight: 200,btnSize:'sm',searchBox: 1}"
                                    name="activity_selected" id="activity_selected">
                                <option value="-1" <%if(activity_selected=="-1"){%>
                                selected
                                <%}%>>所有</option>
                                <%for(activity in activityList){%>
                                <option value="${activity.value}" <%if(activity_selected==activity.value){%>
                                selected
                                <%}%>>${activity.key}</option>
                                <%}%></select>
                        </div>
                    </td>
                    <th class="am-text-middle">大类名称</th>
                    <td>
                        <div class="am-u-sm-10">
                            <input type="text" id="name" class="am-form-field am-input-sm" value="${name!}"/>
                        </div>
                    </td>
                </tr>
                <tr>

                    <th class="am-text-middle">启用状态</th>
                    <td>
                        <div class="am-u-sm-12">
                            <select select data-am-selected="{btnWidth: '100%',maxHeight: 200,btnSize:'sm'}"
                                    name="enabled_selected" id="enabled_selected">
                                <option value="-1" <%if(enabled_selected=="-1"){%>selected<%}%>>所有</option>
                                <option value="1"  <%if(enabled_selected=="1"){%>selected<%}%>>启用</option>
                                <option value="0"  <%if(enabled_selected=="0"){%>selected<%}%>>禁用</option>
                            </select>
                        </div>
                    </td>
                    <th class="am-text-middle">所属机构</th>
                    <td>
                        <div class="am-u-sm-10">
                            <select data-am-selected="{btnSize:'sm',maxHeight: 400,btnWidth:'100%',searchBox: 1}"
                                    name="org_id" id="org_id">
                                <%for(orgNode in orgList){%>
                                <option value="${orgNode.value}"
                                <%if(org_id==orgNode.value){%>
                                selected
                                <%}%>>${orgNode.key}</option>
                                <%}%></select>
                        </div>
                    </td>


                </tr>
                <tr>
                    <td colspan="4">
                        <div align='right'>
                            <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary"
                                    id="submit-btn">搜索
                            </button>
                        </div>
                    </td>

                </tr>

                </tbody>
            </table>
        </form>
    </div>

    <div class="am-u-sm-12 am-u-md-12" style="padding:0 1.6rem 1.6rem 1rem;margin:0;">
        <div class="am-btn-toolbar" style="padding-left:.5rem;">
            <div class="am-btn-group am-btn-group-xs">
                <%if(so.hasPermission("TARIFF_CLASS_ADD")) {%>
                <button type="button" class="am-btn am-btn-default" onclick="location.href='${base}/tariffClass/add'">
                    <span class="am-icon-plus"></span> 新增
                </button>
                <%}%>
                <%if(so.hasPermission("TARIFF_CLASS_DELETE")) {%>
                <button type="button" id="deleteButton" disabled="disabled" class="am-btn am-btn-default"
                        onclick="deleteAll('${base}/tariffClass/delete')"><span class="am-icon-trash-o"></span> 删除
                </button>
                <%}%>


                <%if(so.hasPermission("TARIFF_ENABLE")) {%>
                <button type="button" id="enableButton" class="am-btn am-btn-default"
                        onclick="enableAll('${base}/tariffClass/enable')" disabled>
                    <span class="am-icon-toggle-on"></span>
                    启用
                </button>
                <%}%>
                <%if(so.hasPermission("TARIFF_DISABLE")) {%>
                <button type="button" id="disableButton" class="am-btn am-btn-default"
                        onclick="disableAll('${base}/tariffClass/disable')" disabled>
                    <span class="am-icon-toggle-off"></span>
                    禁用
                </button>
                <%}%>

            </div>
        </div>
    </div>

</div>
<div class="am-g">
    <div class="am-u-sm-12">
        <div class="am-scrollable-horizontal">
            <table class="am-table am-table-striped am-table-hover table-main">
                <thead>
                <tr class="am-text-nowrap">
                    <th class="table-check">
                        <input type="checkbox" id="allCheck"/>
                    </th>
                    <th class="table-title">大类名称</th>
                    <th class="table-title">所属活动</th>
                    <th class="table-title">发布人</th>
                    <th class="table-title">发布人机构</th>
                    <th class="table-title">是否启用</th>
                    <th class="table-title">操作</th>
                </tr>
                </thead>
                <tbody>
                <%for(list in pager.list!){%>
                <tr>
                    <td>
                        <input type="checkbox" name="ids" value="${list.id}">
                    </td>
                    <td>${list.name}</td>
                    <td>${list.activity_name}</td>
                    <td>${list.user_name}</td>
                    <td>${list.org_name}</td>
                    <td>
                        <%if(list.enabled == 1){%>
                        是
                        <%}else{%>
                        否
                        <%}%>
                    </td>
                    <td>
                        <div class="am-btn-toolbar">
                            <div class="am-btn-group am-btn-group-xs">
                                <%if(so.hasPermission("TARIFF_CLASS_EDIT")) {%>
                                <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary"
                                        onclick="location.href='${base}/tariffClass/edit?id=${list.id}'"><span
                                        class="am-icon-pencil-square-o"></span>编辑
                                </button>
                                <%}%>
                            </div>
                        </div>
                    </td>
                </tr>


                <%}%>
                </tbody>
            </table>
        </div>
        <div class="am-cf">
            <!-- 分页 -->
            <%if (pager.list.~size > 0){%>
            <div class="am-fr">
                <% include("/admin/common_pager.tpl"){}%>
            </div>
            <%}else{%>
            <div class="am-kai">
                <h3>没有找到任何记录!</h3>
            </div>
            <%}%>
        </div>

    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var $allCheck = $("#allCheck");// 全选复选框
        var $idsCheck = $('[name=ids]:checkbox');// ID复选框
        var $enableButton = $("#enableButton");// 删除按钮
        var $disableButton = $("#disableButton");// 查询按钮
        $allCheck.checkAll();
        // 全选
        $allCheck.click(function () {
            $allCheck.checkAll();
            var isChecked = $allCheck.is(":checked");
            if (isChecked == false) {
                $enableButton.prop("disabled", true);
                $disableButton.prop("disabled", true);
            } else {
                $enableButton.prop("disabled", false);
                $disableButton.prop("disabled", false);
            }
        });
        // 无复选框被选中时,删除按钮不可用
        $idsCheck.click(function () {
            var $idsChecked = $("[name='ids']:checked");
            if ($idsChecked.size() > 0) {
                $enableButton.prop("disabled", false);
                $disableButton.prop("disabled", false);
            } else {
                $enableButton.prop("disabled", true);
                $disableButton.prop("disabled", true);
            }
        });
    });
    $(function () {
        var keywordsObj = {};

        function setKeyWords() {
            if ($("#org_id").val())
                keywordsObj.org_id = $("#org_id").val();
            if ($("#name").val())
                keywordsObj.name = $("#name").val();
            if ($("#activity_selected").val())
                keywordsObj.activity_selected = $("#activity_selected").val();
            if ($("#enabled_selected").val())
                keywordsObj.enabled_selected = $("#enabled_selected").val();
            var keywords = "";
            if (!$.isEmptyObject(keywordsObj)) {
                keywords = JSON.stringify(keywordsObj);
            }
            console.log("keywords = " + keywords);

            $("#keywords").val(keywords);
        }

        $("#submit-btn").on("click", function () {
            setKeyWords();
            $("#isExport").val("0");
            $("#list-form").submit();
        });

        $("#export-btn").on("click", function () {
            $('#download').hide();
            //loading层
            var index = parent.layer.load(0, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            setKeyWords();
            $("#isExport").val("1");
            //$("#list-form").submit();
            var formParam = $("#list-form").serialize();//序列化表格内容为字符串
            $.ajax({
                type: 'post',
                url: '${base}/vnetReport/list',
                data: formParam,
                success: function (data) {
                    console.log(data);
                    if (data.status == 1) {
                        $('#downloadlink').text(data.message);
                        $('#downloadlink').attr('href', data.url);
                        $('#download').show();
                    } else {
                        $('#downloadlink').text(data.message);
                        $('#download').show();
                    }
                    parent.layer.close(index);
                },
                error: function () {
                    parent.layer.close(index);
                }
            });
        });
    });


    function enableAll(url){
        batchProcess($("#enableButton"), url, '启用','是');
    }

    function disableAll(url){
        batchProcess($("#disableButton"), url, '禁用','否');
    }

    function batchProcess(btnObj, url, actinoName, value) {

        var $idsCheckedCheck = $("[name='ids']:checked");
        var $deleteButton = btnObj;// 删除按钮
        var ids = $idsCheckedCheck.serialize();
        if (confirm('您确定要' + actinoName + '吗？') == true) {
            $.ajax({
                url: url,
                data: ids,
                dataType: "json",
                async: false,
                beforeSend: function (data) {
                    $deleteButton.prop("disabled", true)
                },
                success: function (data) {
                    $deleteButton.prop("disabled", false)
                    if (data.status == "success") {
                        $idsCheckedCheck.parent().parent().find('#tariff_enabled').html(value);
                        if (actinoName == '禁用') {
                            $idsCheckedCheck.parent().parent().find('#tariff_operated').find('#edit-div').removeClass('am-hide');
                        } else if (actinoName == '启用') {
                            $idsCheckedCheck.parent().parent().find('#tariff_operated').find('#edit-div').addClass('am-hide');
                        }
                    }
                    sendMessage({
                        title: actinoName,
                        text: data.message,
                        type: "success",
                        timer: 2000,
                        showConfirmButton: false
                    });
                }
            });
            //cleanCheck();
            location.reload(true);
        }
    }
</script>
<%}%>
<%}%>